/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdRegistrarUnidades.java
 *
 * Created on 26-oct-2010, 9:36:47
 */

package Administracion;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import com.jidesoft.hints.ListDataIntelliHints;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import suministros.ejb30.TO.AreaTO;
import suministros.ejb30.TO.UnidadOrganizativaTO;
import suministros.ejb30.entity.TipoArea;
import suministros.ejb30.entity.UnidadOrganizativa;
import suministros.ejb30.helper.Utiles_Convertir;

/**
 *
 * @author Diana
 */
public class jdRegistrarUnidades extends javax.swing.JDialog {
     private ListDataIntelliHints intelliHintsUnidOrganica;
     private UnidadOrganizativa unid_org_pedido;
    /** Creates new form jdRegistrarUnidades */
    public jdRegistrarUnidades(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.llenarComboArea();
        try {
            this.cargaHintsIndependientes();
        } catch (Exception ex) {
            Logger.getLogger(jdRegistrarUnidades.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     private void cargaHintsIndependientes() throws Exception {
        this.intelliHintsUnidOrganica = new ListDataIntelliHints(this.jtfDesUnOrganica, GestionPedidosDelegate.getInstance().getListaUnidOrganizativasDependencias());

     }
         private void buscarUnidOrganica()
      {
        UnidadOrganizativaTO unoorg;
        if(this.jtfCodUnorganica.getText().trim().length() < 1)
        {
            unoorg = (UnidadOrganizativaTO)intelliHintsUnidOrganica.getSelectedHint();
           
        } else
        {
            unoorg = Listas.getUn_organizativa_codigo(jtfCodUnorganica.getText().trim());
   
        }
        if(unoorg ==null)
        {
            Utiles_MensajesDialogo.mensajeError("La unidad organica no existe!");
            this.jtfDesUnOrganica.setText("");
            unid_org_pedido = null;

        } else
        {
            unid_org_pedido = Utiles_Convertir.convertirToUnidadOrganizativa_UnidadOrganizativaTO(unoorg);
            jtfDesUnOrganica.setText(unoorg.toString());
            jtfCodUnorganica.setText(unoorg.getCod_Uni_Organizativa());

        }
  }

     private void llenarComboArea() {
        List listaTipos1 = null;
        List listaTipos_cbo1 = this.cboBeanBindingArea1.getLista();
        listaTipos_cbo1.clear();
        try
        {
          listaTipos1 =GestionPedidosDelegate.getInstance().getListaAreas();

          this.cboBeanBindingArea1.setLista(listaTipos1);
          this.bindingGroup.bind();
          this.jcbArea.updateUI();
          this.jcbArea.setSelectedIndex(-1);
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener Tipos\n" + ex.getMessage());
          ex.printStackTrace();
          Logger.getLogger(jdRegistrarUnidades.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
     public void guardarUnidades(){
         UnidadOrganizativa unidad= new UnidadOrganizativa();
         unidad.setUnidadOrganizativa(this.jTextField1.getText());
         unidad.setCodUniDepend(this.jtfCodUnorganica.getText());
         int nivel= Integer.parseInt(this.unid_org_pedido.getNivel()+"")+1;
         char niv= (nivel+"").charAt(0);
         unidad.setNivel(niv);
         String area=((AreaTO)this.jcbArea.getSelectedItem()).getCodigo();
         unidad.setCodArea(new TipoArea(area));
            Utiles_MensajesDialogo.mensajeExito();

        try {
            GestionPedidosDelegate.getInstance().insertarUnidadOrganizativa(unidad);
            Utiles_MensajesDialogo.mensajeError("No se pudo relizar la transaccion");
        } catch (Exception ex) {
            Logger.getLogger(jdRegistrarUnidades.class.getName()).log(Level.SEVERE, null, ex);
        }
     }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBeanBindingArea1 = new administracion.BeanBinding.CboBeanBindingArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jcbArea = new javax.swing.JComboBox();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jtfCodUnorganica = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jtfDesUnOrganica = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdRegistrarUnidades.class);
        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jcbArea.setName("jcbArea"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingArea1, eLProperty, jcbArea);
        bindingGroup.addBinding(jComboBoxBinding);

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jbCancelar.setFont(resourceMap.getFont("jbCancelar.font")); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jLabel8.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jtfCodUnorganica.setToolTipText(resourceMap.getString("jtfCodUnorganica.toolTipText")); // NOI18N
        jtfCodUnorganica.setName("jtfCodUnorganica"); // NOI18N
        jtfCodUnorganica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodUnorganicaActionPerformed(evt);
            }
        });

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jtfDesUnOrganica.setName("jtfDesUnOrganica"); // NOI18N
        jtfDesUnOrganica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDesUnOrganicaActionPerformed(evt);
            }
        });
        jtfDesUnOrganica.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDesUnOrganicaKeyPressed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(45, 45, 45)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                                    .addComponent(jcbArea, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfCodUnorganica, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfDesUnOrganica, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                            .addComponent(jLabel1))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar)
                        .addGap(169, 169, 169))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtfCodUnorganica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesUnOrganica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        guardarUnidades();
    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jtfCodUnorganicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodUnorganicaActionPerformed

        if (this.jtfCodUnorganica.getText().trim().length() < 1)
            this.jtfDesUnOrganica.requestFocus();
        else
            buscarUnidOrganica();
}//GEN-LAST:event_jtfCodUnorganicaActionPerformed

    private void jtfDesUnOrganicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDesUnOrganicaActionPerformed

        buscarUnidOrganica();
}//GEN-LAST:event_jtfDesUnOrganicaActionPerformed

    private void jtfDesUnOrganicaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDesUnOrganicaKeyPressed

        if (this.jtfCodUnorganica.getText().trim().length() > 0)
            this.jtfCodUnorganica.setText("");
}//GEN-LAST:event_jtfDesUnOrganicaKeyPressed

    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private administracion.BeanBinding.CboBeanBindingArea cboBeanBindingArea1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    public javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JComboBox jcbArea;
    private javax.swing.JTextField jtfCodUnorganica;
    private javax.swing.JTextField jtfDesUnOrganica;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
